import { _decorator, Button, Component, Enum, EventTouch, find, instantiate, JsonAsset, Label, Node, Prefab, Sprite, SpriteFrame } from 'cc';
import { BULLET, MAXPROPERTY, WEAPON } from '../AshFramework/Datas/Constant';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { PrefsManager } from '../AshFramework/Managers/PrefsManager';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { BundleManager, Bundles } from '../AshFramework/Managers/BundleManager';
import { GameData } from './GameData';
import { GameManager } from './GameManager';
import { HintController } from './HintController';
import { Shop } from './Shop';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('BulletItem')
export class BulletItem extends Component {

    @property({ type: Enum(BULLET) })
    BulletType: BULLET = BULLET.软铅弹头9x19mm;

    // @property({ type: Enum(WEAPON), displayName: "归属于" })
    BelongTo: WEAPON[] = [];

    @property
    Price: number = 0;

    @property(Node)
    Have: Node = null;

    @property(Node)
    NoHave: Node = null;

    Name: string = "";
    NameLabel: Label = null;
    IconSprite: Sprite = null;
    HarmLabel: Label = null;
    ArmorPenetrationLabel: Label = null;
    PriceLabel: Label = null;
    NumLabel: Label = null;

    HarmSprite: Sprite = null;
    APSprite: Sprite = null;

    Num: number = 1;

    private _isClick: boolean = false;//能否点击

    init(name: string, json: any) {
        this.NameLabel = find("name", this.node).getComponent(Label);
        this.IconSprite = find("Icon", this.node).getComponent(Sprite);
        this.NumLabel = find("数量", this.node).getComponent(Label);
        this.HarmLabel = find("伤害/伤害", this.node).getComponent(Label);
        this.ArmorPenetrationLabel = find("破甲/破甲", this.node).getComponent(Label);
        this.PriceLabel = find("价格/价格", this.node).getComponent(Label);

        this.HarmSprite = find("伤害/进度条底部/进度条", this.node).getComponent(Sprite);
        this.APSprite = find("破甲/进度条底部/进度条", this.node).getComponent(Sprite);

        this.NameLabel.string = name;
        BundleManager.LoadSpriteFrame(Bundles.BundleSprites, "子弹/" + name).then((spriteFrame: SpriteFrame) => {
            this.IconSprite.spriteFrame = spriteFrame;
        })

        this.Name = name;
        this.Num = json.num;
        if (this.Num != 1) this.NumLabel.string = "X" + this.Num;
        const keys: string[] = json.belongTo;
        for (let index = 0; index < keys.length; index++) {
            const key = keys[index];
            this.BelongTo.push(WEAPON[key]);
        }
        const harm = json.harm;
        const ap = json.armorPenetration;
        this.Price = json.price;
        this.HarmLabel.string = harm.toString();
        this.ArmorPenetrationLabel.string = ap.toString();
        this.PriceLabel.string = this.Price.toString();

        this.HarmSprite.fillRange = harm / MAXPROPERTY.HARM;
        this.APSprite.fillRange = ap / MAXPROPERTY.AP;

        this.Have.on(Node.EventType.TOUCH_END, this.onHaveTouchEnd, this);
        this.NoHave.on(Node.EventType.TOUCH_END, this.onNoHaveTouchEnd, this);
        this.show();
    }

    initGrenade(name: string) {
        this.Name = name;
        this.Have.on(Node.EventType.TOUCH_END, this.onHaveTouchEnd, this);
    }

    show() {
        for (let index = 0; index < this.BelongTo.length; index++) {
            const belongTo = this.BelongTo[index];
            if (PrefsManager.Instance.userData.HaveWeapon.findIndex(e => e == belongTo) != -1) {
                this._isClick = true;
                this.Have.active = true;
                this.NoHave.active = false;
                return;
            } else {
                this._isClick = false;
                this.Have.active = false;
                this.NoHave.active = true;
            }
        }
    }

    onHaveTouchEnd(event: EventTouch) {
        AudioManager.PlaySound(Audios.ButtonClick);
        if (PrefsManager.Instance.userData.Gold >= this.Price) {
            GameData.addBulletByName(this.Name, this.Num);
            Shop.Instance.showGold(this.Price);
            PrefsManager.Instance.saveData();
            ResourceUtil.LoadPrefab("UI/Hint").then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = GameManager.Instance.Canvas;
                const bulletNum: number = GameData.getBulletNumByName(this.Name);
                const hint: string = `购买成功！ ${this.Name}: X${bulletNum}`;
                node.getComponent(HintController).showHint(hint);
            })
        } else {
            ResourceUtil.LoadPrefab("UI/Hint").then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = GameManager.Instance.Canvas;
                const hint: string = `余额不足！`;
                node.getComponent(HintController).showHint(hint);
            })
        }
    }
    onNoHaveTouchEnd(event: EventTouch) {
        ResourceUtil.LoadPrefab("UI/Hint").then((prefab: Prefab) => {
            const node: Node = instantiate(prefab);
            node.parent = GameManager.Instance.Canvas;
            let hint: string = "没有武器：";
            for (let index = 0; index < this.BelongTo.length; index++) {
                const weapon: WEAPON = this.BelongTo[index];
                const weaponName: string = ZTool.GetEnumKeyByValue(WEAPON, weapon);
                if (index == 0) {
                    hint += weaponName;
                } else {
                    hint += "或者" + weaponName;
                }
            }
            node.getComponent(HintController).showHint(hint);
        })
    }

    protected onEnable(): void {
        if (this.BulletType == BULLET.高爆手雷 || this.BulletType == BULLET.破片手雷) return;
        this.show();
    }
}


